package tingche.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tingche.bean.CarTingBean;
import util.JDBCUtils;

public class CarTingDao {
	Connection con=null;
	PreparedStatement ps=null;
	public int insert(CarTingBean ting) {
		String sql="insert into c_ting(stat) values(?)";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, ting.getStat());
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public CarTingBean selectByHao(CarTingBean ting) {
		String sql="select * from c_ting where id=?";
		ResultSet rs=null;
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, ting.getId());
			rs=ps.executeQuery();
			rs.next();
				CarTingBean c=new CarTingBean();
				c.setId(rs.getInt("id"));
				c.setStat(rs.getInt("stat"));
				return c;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public int updatestat(CarTingBean ting) {
		String sql="update c_ting set stat=? where id=?";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, ting.getStat());
			ps.setInt(2, ting.getId());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	public List<CarTingBean> selectAll(){
		String sql="select * from  c_ting ";
		List<CarTingBean> list=new ArrayList<>();
		ResultSet rs=null;
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				CarTingBean ting=new CarTingBean();
				ting.setId(rs.getInt("id"));
				ting.setStat(rs.getInt("stat"));
				list.add(ting);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
